﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;

namespace PTTPOS_Solution.DAL
{
    public static class Menu_DA
    {
        public static DataTable ListMenu=new DataTable();
        public static void GetListMenu(int status2)
        {
            MySqlCommand com = new MySqlCommand("select * from tblmenus where status <> 0 and status2=@status2", Connection_PTTPOS.GetConnection());
            com.Parameters.AddWithValue("@status2", status2);
            com.Connection.Open();
            MySqlDataAdapter da = new MySqlDataAdapter(com);
            ListMenu.Clear();
            da.Fill(ListMenu);
            com.Connection.Close();
        }

        public static void UpdateMenu(int id, int Status)
        {
            MySqlCommand com = new MySqlCommand("update tblmenus set Status = @Status where id=@id", Connection_PTTPOS.GetConnection());
            com.Parameters.AddWithValue("@id", id);
            com.Parameters.AddWithValue("@Status", Status);
            com.Connection.Open();
            com.ExecuteNonQuery();
            com.Connection.Close();
        }

        public static string findGroupitemByMenu(string id_menu)
        {
            string str = "";
            MySqlCommand com = new MySqlCommand("select * from tblmenus where id=@id_menu and status =1", Connection_PTTPOS.GetConnection());
            com.Parameters.AddWithValue("@id_menu", id_menu);
            com.Connection.Open();
            MySqlDataReader reader = com.ExecuteReader();
            if (reader.Read())
            {
                str = reader["id_groupitems"].ToString();
            }
            com.Connection.Close();
            return str;
        }

        public static DataTable Find(string name, string id)
        {
            DataTable tbl = new DataTable();
            MySqlCommand com = new MySqlCommand("select * from tblmenus where status <> 0 and id=@id", Connection_PTTPOS.GetConnection());
            com.Parameters.AddWithValue("@id", id);
            MySqlCommand com2 = new MySqlCommand("select * from tblmenus where status <> 0 and Name like concat('%',@Name,'%')", Connection_PTTPOS.GetConnection());
           
            com2.Parameters.AddWithValue("@Name", name);
            com.Connection.Open();
            com2.Connection.Open();
            MySqlDataAdapter da = new MySqlDataAdapter(com);
            MySqlDataAdapter da2 = new MySqlDataAdapter(com2);
            da2.Fill(tbl);
            da.Fill(tbl);
            com.Connection.Close();
            com2.Connection.Close();
            return tbl;
        }
    }
}
